package org.dromara.system.domain.vo;

import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.system.domain.WbCandidateList;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import java.util.Map;


/**
 * 中标候选人详情视图对象 wb_candidate_list
 *
 * @author Lion Li
 * @date 2024-05-27
 */
@Data
@ExcelIgnoreUnannotated
@AutoMapper(target = WbCandidateList.class)
public class WbCandidateListRiskVo implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */

    private Long id;

    /**
     * 候选人公示id
     */

    private Long candidateId;

    /**
     * No.
     */
    @ExcelProperty(value = "序号",index = 0)
    private String no;

    /**
     * 采购项目名称
     */
    private String candidateName;

    /**
     * 采购项目编号
     */
    private String candidateNo;

    /**
     * 分标名称
     */
    @ExcelProperty(value = "分标名称",index = 1)
    private String subStandardName;

    /**
     * 分标编号
     */
    private String subStandardId;

    /**
     * 包名称
     */
    private String bagName;

    /**
     * 包号
     */
    @ExcelProperty(value = "分包编号",index = 2)
    private String bagNo;

    /**
     * 轮次
     */
    private String round;

    /**
     * 项目单位
     */
    private String itemUnit;

    /**
     * 推荐的中标候选人
     */
    @ExcelProperty(value = "投标厂商名称" ,index = 3)
    private String candidate;

    private String candidateCode;

    /**
     * 折扣率
     */
    private String bagDiscount;

    /**
     * 价格
     */
    private String priceOrDiscount;

    /**
     * 投标保证金编号
     */
    private String priceDescription;

    /**
     * 质量(售后服务承诺)
     */

    private String quality;

    /**
     * 交货期
     */

    private String delivery;

    /**
     * 排序
     */

    private String sort;

    /**
     * 资格能力
     */

    private String qualified;

    /**
     * 评标情况
     */

    private String bidCase;

    /**
     * 备注
     */
    private String remark;

    /**
     * 查验时间
     */
    @ExcelProperty(value = "查验时间" ,index = 4)
    private String publicityTime;

    /**
     * 省份
     */
    private String provinceName;

    /**
     * 标题
     */
    private String headline;

    /**
     * 年份
     */
    private String year;


    /**
     *
     */
    @ExcelProperty(value = "围串标风险预警标志" ,index = 5)
    private String isRisk;

    /**
     *
     */
    @ExcelProperty(value = "经营异常预警" ,index = 6)
    private String abnormalRisk;

    /**
     *
     */
    @ExcelProperty(value = "围串标风险预警内容" ,index = 7)
    private String riskInfo;

    /**
     *
     */
    @ExcelProperty(value = "异常数量" ,index = 24)
    private String riskTotal;

    //√
    @ExcelProperty(value = "是否为母子公司" ,index = 8)
    private String isParent;

    //√
    @ExcelProperty(value = "是否为同一家母公司的子公司" ,index = 12)
    private String isSameParent;

    //√
    @ExcelProperty(value = "是否为祖孙公司" ,index = 14)
    private String isGrantParent;

    //√
    @ExcelProperty(value = "是否为母子公司（非控股）" ,index = 20)
    private String isParentNo;

    //√
    @ExcelProperty(value = "股东(占比)" ,index = 21)
    private String invest;

    //√
    @ExcelProperty(value = "股东重复" ,index = 11)
    private String isSameShareholder;

    //
    @ExcelProperty(value = "是否存在相互控股关系" ,index = 10)
    private String isCrossShareholding;

    //
    @ExcelProperty(value = "人员(最终受益人/实际控制人）" ,index = 23)
    private String people;

    //√
    @ExcelProperty(value = "是否存在相同股东（非控股）/受益人" ,index = 13)
    private String isSameBeneficiary;

    //√
    @ExcelProperty(value = "法定代表人" ,index = 22)
    private String legalRepresentative;

    //
    @ExcelProperty(value = "法定代表人是否相同" ,index = 9)
    private String isSameLegalRepresentative;

    //
    @ExcelProperty(value = "法人是否为其他公司股东" ,index = 18)
    private String legalIsShareholder;

    //
    @ExcelProperty(value = "法人是否为其他公司最终受益人/实际控制人" ,index = 19)
    private String legalIsBeneficiary;

    //√
    @ExcelProperty(value = "注册地址" ,index = 25)
    private String address;

    //√
    @ExcelProperty(value = "企业联系方式" ,index = 26)
    private String contactPhone;

    //√
    @ExcelProperty(value = "邮箱" ,index = 27)
    private String contactEmail;

    //
    @ExcelProperty(value = "注册地址是否相同" ,index = 15)
    private String isSameAddress;

    //
    @ExcelProperty(value = "企业联系方式是否相同" ,index = 16)
    private String isSamePhone;

    //
    @ExcelProperty(value = "邮箱是否相同" ,index = 17)
    private String isSameEmail;

    private int abnormalTotal;
    private int illegalTotal;
    private int dishonestTotal;
    private int punishmentTotal;

    /**
     * 处理法人
     */
    public void setSameLegalPerson(List<String> codeList, Map<String, String> candidateCodeMap) {
        for(String candidateCode : codeList) {
            if(this.candidateCode.equals(candidateCode)) {
                continue;
            }

            if(StringUtils.isNotEmpty(this.isSameLegalRepresentative)) {
                this.isSameLegalRepresentative += ";";
            } else {
                this.isSameLegalRepresentative = "";
            }

            this.isSameLegalRepresentative += candidateCodeMap.get(candidateCode);
        }

        setRisk("与" + this.isSameLegalRepresentative + "法人相同");
    }

    public void setSameMail(List<String> codeList, Map<String, String> candidateCodeMap) {
        for(String candidateCode : codeList) {
            if(this.candidateCode.equals(candidateCode)) {
                continue;
            }

            if(StringUtils.isNotEmpty(this.isSameEmail)) {
                this.isSameEmail += ";";
            } else {
                this.isSameEmail = "";
            }

            this.isSameEmail += candidateCodeMap.get(candidateCode);
        }

        setRisk("与" + this.isSameEmail + "邮箱相同");
    }

    public void setSameAddress(List<String> codeList, Map<String, String> candidateCodeMap) {
        for(String candidateCode : codeList) {
            if(this.candidateCode.equals(candidateCode)) {
                continue;
            }

            if(StringUtils.isNotEmpty(this.isSameAddress)) {
                this.isSameAddress += ";";
            } else {
                this.isSameAddress = "";
            }

            this.isSameAddress += candidateCodeMap.get(candidateCode);
        }

        setRisk("与" + this.isSameAddress + "注册地址相同");
    }

    public void setSameContact(List<String> codeList, Map<String, String> candidateCodeMap) {
        for(String candidateCode : codeList) {
            if(this.candidateCode.equals(candidateCode)) {
                continue;
            }

            if(StringUtils.isNotEmpty(this.isSamePhone)) {
                this.isSamePhone += ";";
            } else {
                this.isSamePhone = "";
            }

            this.isSamePhone += candidateCodeMap.get(candidateCode);
        }

        setRisk("与" + this.isSamePhone + "联系电话相同");
    }

    //设置母子公司关系
    public void setParent(SkyRelationshipVo vo) {
        if("invest_c".equalsIgnoreCase(vo.getDataType()) && lessThan50(vo.getPercent())) {
            //小于0.5，非控股股东
            if(StringUtils.isNotEmpty(this.isParentNo)) {
                this.isParentNo += ";";
            } else {
                this.isParentNo = "";
            }

            this.isParentNo += (vo.getEndName() + "("+vo.getName() + ")");
            setRisk("与" + vo.getEndName() + "存在非控股母子公司关系");
        } else {
            //分公司或控股股东，也就是子公司
            if(StringUtils.isNotEmpty(this.isParent)) {
                this.isParent += ";";
            } else {
                this.isParent = "";
            }

            this.isParent += (vo.getEndName() + "("+vo.getName() + ")");
            setRisk("与" + vo.getEndName() + "存在母子公司关系");
        }
    }

    //设置祖孙关系
    public void setGrantParent(SkyRelationshipVo start, SkyRelationshipVo end) {
        if("invest_c".equalsIgnoreCase(start.getDataType()) && lessThan50(start.getPercent())) {
            return;
        }

        if("invest_c".equalsIgnoreCase(end.getDataType()) && lessThan50(end.getPercent())) {
            return;
        }

        //祖公司对母公司超过50%且母公司对子公司超过50%
        if(StringUtils.isNotEmpty(this.isGrantParent)) {
            this.isGrantParent += ";";
        } else {
            this.isGrantParent = "";
        }

        this.isGrantParent += end.getEndName();
        setRisk("与" + end.getEndName() + "存在祖孙公司关系");
    }

    public void setSame(SkyRelationshipVo start, SkyRelationshipVo end) {
        String type = getType(start) + getType(end);

        switch (type) {
            case "11" :
            case "12" :
            case "21" :
                setSameParent(end);
                break;
            case "22" :
                setSameParent(end);
                setSameInvest(end);
                break; //都是分支或控股子公司
            case "23" :
            case "32" :
            case "33" :
                setSameInvest(end);
                break; //都是控股人
            case "44" :
                setSameBeneficiary(end);
                break; //都是非控股股东
            default: break;
        }
    }

    //相同母公司
    public void setSameParent(SkyRelationshipVo vo) {
        //子公司
        if(StringUtils.isNotEmpty(this.isSameParent)) {
            this.isSameParent += ";";
        } else {
            this.isSameParent = "";
        }

        this.isSameParent += vo.getStartName();
        setRisk("存在相同母公司: " + vo.getStartName());
    }

    //相同股东
    public void setSameInvest(SkyRelationshipVo vo) {
        if(StringUtils.isNotEmpty(this.isSameShareholder)) {
            this.isSameShareholder += ";";
        } else {
            this.isSameShareholder = "";
        }

        //记录重复股东名字
        this.isSameShareholder += vo.getStartName();
        setRisk("存在股东重复: " + vo.getStartName());
    }

    //相同股东(非控股)
    public void setSameBeneficiary(SkyRelationshipVo vo) {
        if(StringUtils.isNotEmpty(this.isSameBeneficiary)) {
            this.isSameBeneficiary += ";";
        } else {
            this.isSameBeneficiary = "";
        }

        //记录重复股东(非控股)名字
        this.isSameBeneficiary += vo.getStartName();

        setRisk("存在股东(非控股)重复: " + vo.getStartName());
    }

    /**
     *
     * @param vo 数据
     * @return type 1:分支； 2:控股股东(企业),，母公司；3:控股股东(个人)；4:非控股股东
     */
    private String getType(SkyRelationshipVo vo) {
        //dataType = branch ：子公司
        if("branch".equalsIgnoreCase(vo.getDataType())) {
            return "1";
        }

        // or invest_c >= 0.5 ：分支或子公司；并且是控股股东
        if("invest_c".equalsIgnoreCase(vo.getDataType())) {
            return lessThan50(vo.getPercent()) ? "4" : "2";
        }

        //dataType = invest_h >= 0.5 ： 股东(控股)
        if("invest_h".equalsIgnoreCase(vo.getDataType()) && !lessThan50(vo.getPercent())) {
            return "3";
        }

        //dataType = invest_c < 0.5 or invest_h < 0.5 ： 股东(非控股)
        return "4";
    }

    private boolean lessThan50(String percent) {
        return Double.parseDouble(percent) < 0.5D;
    }

    private void setRisk(String riskInfo) {
        if(StringUtils.isNotEmpty(this.riskInfo)) {
            this.riskInfo += "; ";
        } else {
            this.isRisk = "投标供应商基本信息异常"; //风险标志  投标供应商基本信息重复
            this.riskInfo = "";
        }

        this.riskInfo += riskInfo;
    }
}
